<template>
  <div>
    <el-dialog title="编辑" v-model="visible" width="960px" draggable>
      <el-form ref="formRef" :model="formData" label-position="right" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户类型" prop="chargeFlag">
              <el-select v-model="formData.chargeFlag" placeholder="请选择">
                <el-option v-for="item in getEnumBySn('chargeFlag')" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优先级" prop="priority">
              <el-select v-model="formData.priority" placeholder="请选择">
                <el-option v-for="item in getEnumBySn('priority')" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="处理人" prop="handleUserNo">
              <el-select v-model="formData.handleUserNo" placeholder="请选择" filterable>
                <el-option v-for="item in userList" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="handleStatus">
              <el-select v-model="formData.handleStatus">
                <el-option v-for="item in getEnumBySn('handleStatusEnum')" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合作编号" prop="indexId" >
              <el-select v-model="formData.indexId" filterable>
                <el-option v-for="item in agentList" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接单日期" prop="receiveDate">
              <el-date-picker value-format="YYYY-MM-DD" v-model="formData.receiveDate"
                              placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户姓名" prop="customName">
              <el-input v-model="formData.customName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="formData.sex">
                <el-option v-for="item in getEnumBySn('sexEnum')" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="平台名称" prop="debtType">
              <el-input v-model="formData.debtType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="诉求" prop="appeal">
              <el-input v-model="formData.appeal"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最新处理情况" prop="handleProcessMemo">
              <el-input v-model="formData.handleProcessMemo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="群名手机号" prop="receivePhone">
              <el-select v-model="formData.receivePhone" clearable filterable>
                <el-option v-for="item in phoneList" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否交付" prop="deliverFlag">
              <el-select v-model="formData.deliverFlag" >
                <el-option v-for="item in getEnumBySn('deliverFlag')" :label="item.label"
                           :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有群" prop="hasGroupFlag">
              <el-select v-model="formData.hasGroupFlag">
                <el-option v-for="item in getEnumBySn('Yes_No')" :label="item.label" :key="item.value"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="12">
          <el-form-item label="进线手机号" prop="comePhone" required>
            <el-select v-model="formData.comePhone"  filterable clearable>
              <el-option v-for="item in phoneList" :label="item.label" :key="item.value"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col :span="24">
            <el-form-item label="贴纸备注" prop="memo" required>
              <el-input type="textarea" rows="10" v-model="formData.memo"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="submit" :loading="saveLoading">确 定</el-button>
        </span>
      </template>

    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {getEnumBySn, getIndexIdList, getPhoneList, getUserList} from "@/api/enums";
const visible = ref(false)
const formRef = ref()
const formData = ref({})
const props = defineProps({
  currentItem: {
    type: Object,
    required: true,
  },
});
import request from '@/config/axios'
const emit = defineEmits(["ok"])

const userList  = ref([])
const agentList = ref([])
const phoneList = ref([])
const saveLoading = ref(false)

onMounted(() => {
  getUserList().then(res => {
    userList.value = res
  })
  getIndexIdList().then(res => {
    agentList.value = res
  })
  getPhoneList().then(res => {
    phoneList.value = res
  })
});

// 保存
const submit = function () {
  if (formData.value.id) {
    edit()
  } else {
    add()
  }
}

//新增
const add = function () {
  formRef.value.validate((valid) => {
    if (!valid) {
      return
    }
    saveLoading.value = true
    request.post({
      url: '/negotiation/save',
      data: formData.value
    }).then(res => {
      visible.value = false
      emit("ok")
    }).finally(() => {
      saveLoading.value = false
    })
  })

}

//编辑
const edit = function () {
  formRef.value.validate((valid) => {
    if (!valid) {
      return
    }
    saveLoading.value = true
    request.post({
      url: '/negotiation/updateById',
      data: formData.value
    }).then(res => {
      visible.value = false
      emit("ok")
    }).finally(() => {
      saveLoading.value = false
    })
  })
}


defineExpose({
  add: (rowData) => {
    visible.value = true
    formData.value = {}
  },
  edit: (rowData) => {
    visible.value = true
    formData.value = rowData
  }
})
</script>


<style scoped></style>
